<template>
  <div>
    <el-dialog
      :title="title"
      :visible="dialogFormVisible"
      @close="closeDialog"
      @open="openDialog"
    >
      <el-form ref="addUser" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item v-if="!this.id" label="密码" prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="form.role" />
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="form.permission_group_id" placeholder="请选择">
            <el-option
              v-for="item in permission_group_ids"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="联系电话
"
        >
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="介绍">
          <el-input v-model="form.introduction" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitAdduser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUserApi, getUserDetailApi, editUserDetailApi } from '@/api/userList'
import { getPermissonSimpleListApi } from '@/api/permisson'
export default {
  props: {
    dialogFormVisible: Boolean
  },
  data() {
    return {
      /*      pswHidden: true, */
      permission_group_ids: [], // 权限点数组
      id: '',
      form: {
        avatar: '', // 账号
        role: '', // 角色
        phone: '', // 联系电话
        permission_group_id: '', //	权限组id
        email: '', // 邮件
        password: '',
        introduction: '', // 介绍
        username: '' // 用户名
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户姓名不能为空',
            trigger: ['blur', 'change']
          }
        ],
        email: [
          {
            required: true,
            message: '邮箱不能为空',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.id ? '编辑用户' : '创建用户'
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:dialogFormVisible', false)
      this.form = {
        avatar: '', // 头像
        role: '', // 角色
        phone: '', // 联系电话
        permission_group_id: '', //	权限组id
        email: '', // 邮件
        password: '',
        introduction: '', // 介绍
        username: '' // 用户名
      }
      this.id = ''
    },
    async openDialog(id, val) {
      const { data } = await getPermissonSimpleListApi()
      this.permission_group_ids = data
      if (val === 'edit') {
        const { data } = await getUserDetailApi(id)
        this.form = data
        this.id = id
      }
    },
    async submitAdduser() {
      this.$refs.addUser.validate(async (flag) => {
        if (!flag) return
        if (this.id) {
          this.form = {
            avatar: this.form.avatar,
            email: this.form.email,
            introduction: this.form.introduction,
            permission_group_id: this.form.permission_group_id,
            phone: this.form.phone,
            role: this.form.role,
            username: this.form.username
          }
          await editUserDetailApi(this.id, this.form)
          this.$message.success('修改成功')
        } else {
          await addUserApi(this.form)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.$emit('addUser')
      })
    }
  }
}
</script>

<style></style>
